﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenIotMain.SQLiteTool {
    public class SQLiteHelper {
        private SQLiteConnection connection;

        public SQLiteHelper(string dbPath) {
            connection = new SQLiteConnection($"Data Source={dbPath};Version=3;");
            connection.Open();
        }

        public void Close() {
            connection.Close();
        }

        public void ExecuteNonQuery(string sql) {
            using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                command.ExecuteNonQuery();
            }
        }

        public object ExecuteScalar(string sql) {
            using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                return command.ExecuteScalar();
            }
        }

        public SQLiteDataReader ExecuteReader(string sql) {
            using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                return command.ExecuteReader();
            }
        }

        public DataTable ExecuteDataTable(string sql) {
            using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(command)) {
                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);
                    return dataTable;
                }
            }
        }
    }
}
